import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:image_picker/image_picker.dart';
import 'package:imember_printer/imember_printer.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String content = """
  TextText    TextText  TextText\n
  繁體字繁體字繁體字繁體字\n
  繁體字繁體字繁體字\n\n
  繁體字繁體字繁體字\n
  繁體字繁體字繁體字\n
  """;

  @override
  void initState() {
    super.initState();
  }

  final ImagePicker _picker = ImagePicker();

  printImage_tp809() async {
    final XFile? image = await _picker.pickImage(source: ImageSource.gallery);
    if (image?.name != null) {
      TP809.printImage(filePath: image?.path ?? "", size: 576).then((value) => print("print  success")).catchError((e) {
        print(e.toString());
      });
    }
  }

  printImage_ht300() async {
    final XFile? image = await _picker.pickImage(source: ImageSource.gallery);
    if (image?.name != null) {
      HT300.printImage(filePath: image?.path ?? "").then((value) => print("print  success")).catchError((e) {
        print(e.toString());
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
            appBar: AppBar(title: const Text('Plugin example app')),
            body: Column(children: [
              Text("TP809"),
              Button("connect", () {
                TP809.connectPrinter(ipAddress: "192.168.11.53", port: "9100").then((value) => print("connect success")).catchError((e) {
                  print(e.toString());
                });
              }),
              Button("disconnect", () {
                TP809.disconnectPrinter().then((value) => print("disconnect success")).catchError((e) {
                  print(e.toString());
                });
              }),
              Button("print Text", () {
                TP809.isConnect().then((value) => print("$value"));
                // TP809.printText(text: content).then((value) => print("print text success")).catchError((e) {
                //   print(e.toString());
                // });
              }),
              Button("print Image", printImage_tp809),
              Button("open cashDrawer", () {
                TP809.openCashDrawer().then((value) => print("success")).catchError((e) {
                  print(e.toString());
                });
              }),
              Text("HT300"),
              Button("connect", () {
                HT300.connectPrinter(ipAddress: "192.168.11.102", port: "9100").then((value) => print("connect success")).catchError((e) {
                  print(e.toString());
                });
              }),
              Button("disconnect", () {
                HT300.disconnectPrinter().then((value) => print("disconnect success")).catchError((e) {
                  print(e.toString());
                });
              }),
              Button("print Text", () {
                HT300.printText(text: content).then((value) => print("print text success")).catchError((e) {
                  print("print text fail:" + e.toString());
                });
              }),
              Button("print Image", printImage_ht300),
            ])));
  }
}

class Button extends StatelessWidget {
  String text;
  Function() onTap;

  Button(this.text, this.onTap);

  @override
  Widget build(BuildContext context) {
    return TextButton(
        onPressed: onTap, child: Container(height: 36, width: 120, alignment: Alignment.center, color: Colors.lime, child: Text("$text")));
  }
}
